package com.qianfeng.teachdemo.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.internal.Utils;
import com.lidroid.other.UtilsTextStyle;
import com.lidroid.verification.Rule;
import com.lidroid.verification.Validator;
import com.lidroid.verification.annotation.Checked;
import com.lidroid.verification.annotation.ConfirmPassword;
import com.lidroid.verification.annotation.Email;
import com.lidroid.verification.annotation.IpAddress;
import com.lidroid.verification.annotation.NumberRule;
import com.lidroid.verification.annotation.Password;
import com.lidroid.verification.annotation.Regex;
import com.lidroid.verification.annotation.Required;
import com.lidroid.verification.annotation.TextRule;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.qianfeng.teachdemo.R;

public class ValidationFragment extends Fragment implements Validator.ValidationListener {
    @Password(message = "请输入密码", order = 1)
    @ViewInject(R.id.a)
    EditText a;
    @ConfirmPassword(messageResId = R.string.err, order = 2)
    @ViewInject(R.id.b)
    EditText b;
    @Email(empty = false, message = "邮箱格式错误", order = 3)
    @ViewInject(R.id.c)
    EditText c;
    @IpAddress(message = "IP格式错误", order = 4)
    @ViewInject(R.id.d)
    EditText d;
    @NumberRule(type = NumberRule.NumberType.INTEGER, lt = 1000, gt = 1, message = "输入数字错误", order = 5)
    @ViewInject(R.id.e)
    EditText e;
    @Regex(message = "输入内容错误", trim = true, pattern = "[a-zA-Z0-9_]{6,15}", order = 6)
    @ViewInject(R.id.f)
    EditText f;
    @Required(message = "不能为空", order = 7)
    @ViewInject(R.id.g)
    EditText g;
    @TextRule(maxLength = 4, minLength = 2, message = "文字长度错误", order = 8)
    @ViewInject(R.id.h)
    EditText h;
    @Checked(checked = true, message = "必须选中服务条款", order = 9)
    @ViewInject(R.id.checkbox1)
    CheckBox checkbox1;

    Validator validator;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_validation, container, false);
        ViewUtils.inject(this, rootView);
        return rootView;
    }

    @OnClick(R.id.judge)
    public void click(View view) {
        //这里是做验证
        validator = new Validator(this);
        validator.setValidationListener(this);
        validator.validate();
    }

    @Override
    public void onValidationSucceeded() {
        Toast.makeText(getActivity(), "验证通过", Toast.LENGTH_SHORT).show();
    }

    /**
     * 提示的形式可以自己定义
     */
    @Override
    public void onValidationFailed(View failedView, Rule<?> failedRule) {
        String message = failedRule.getFailureMessage();
        if (failedView instanceof EditText) {
            failedView.requestFocus();
            UtilsTextStyle handler_TextStyle = new UtilsTextStyle();
            handler_TextStyle.setString(message);
            handler_TextStyle.setBackgroundColor(Color.RED, 0, message.length());
            ((EditText) failedView).setError(handler_TextStyle.getSpannableString());
        } else {
            Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
        }
    }
}


